import React, { Component } from 'react'

const MsgContext = React.createContext()

class Son extends Component {
  static contextType = MsgContext
  render() {
    return (
      <div>
        <h3>儿子</h3>
        {this.context}
      </div>
    )
  }
}

// Son.contextType = MsgContext

class Parent extends Component {
  render() {
    return (
      <div>
        <h2>爸爸</h2>
        <Son />
      </div>
    )
  }
}

export default class App extends Component {
  state = {
    msg: '爷爷给的糖'
  }
  render() {
    return (
      <div>
        <h1>爷爷</h1>
        <MsgContext.Provider value={this.state.msg}>
          <Parent />
        </MsgContext.Provider>
      </div>
    )
  }
}
